﻿//***********************************************************************
// Assembly         : phpmk
// Author           : sy
// Created          : 04-02-2010
//
// Last Modified By : sy
// Last Modified On : 04-02-2010
//
// Copyright        : (c) Another-D-Mention Software. All rights reserved.
//***********************************************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Text.RegularExpressions;

namespace Phpmk.ADM.Parameters {
    class ParameterParser {

        public Dictionary<String, StringDictionary> Commands;
        public Dictionary<String, StringDictionary> Plugins;
        
        public ParameterParser(string[] args) {
            this.Commands = new Dictionary<String, StringDictionary>();
            this.Plugins = new Dictionary<String, StringDictionary>();

            Regex spliter = new Regex(@"^-{1,2}|^/|=|:",
                RegexOptions.IgnoreCase | RegexOptions.Compiled);

            Regex remover = new Regex(@"^['""]?(.*?)['""]?$",
                RegexOptions.IgnoreCase | RegexOptions.Compiled);

            string[] parts;
            string currentCommand = "main";
            string currentPlugin = "";
            string parameter = null;

            StringDictionary parameters = new StringDictionary();

            foreach (string txt in args) {
                parts = spliter.Split(txt, 3);
                if (txt.StartsWith("-", StringComparison.CurrentCulture)) {
                    
                    if (!String.IsNullOrEmpty(currentCommand)) {
                        this.Commands.Add(currentCommand, parameters);
                    }
                    if (!String.IsNullOrEmpty(currentPlugin)) {
                        this.Plugins.Add(currentPlugin, parameters);
                    }
                   
                    currentCommand = currentPlugin = "";
                   
                    if (txt.StartsWith("--", StringComparison.CurrentCulture)) {
                        currentPlugin = remover.Replace(parts[1], "$1");
                    } else {
                        currentCommand = remover.Replace(parts[1], "$1");
                    }
                    
                    parameters = new StringDictionary();
                } else {
                    switch (parts.Length) {
                        case 1:
                            if (parameter != null) {
                                if (!parameters.ContainsKey(parameter)) {
                                    parts[0] =
                                        remover.Replace(parts[0], "$1");
                                    parameters.Add(parameter, parts[0]);
                                }
                                parameter = null;
                            } else {
                                parameters.Add("target", txt);
                            }
                            break;
                        case 2:
                            if (parameter != null) {
                                if (!parameters.ContainsKey(parameter))
                                    parameters.Add(parameter, "true");
                            }
                            parameter = parts[1];
                            break;
                        case 3:
                            if (parameter != null) {
                                if (!parameters.ContainsKey(parameter))
                                    parameters.Add(parameter, "true");
                            }
                            parameter = parts[1];
                            if (!parameters.ContainsKey(parameter)) {
                                parts[2] = remover.Replace(parts[2], "$1");
                                parameters.Add(parameter, parts[2]);
                            }
                            parameter = null;
                            break;
                    }
                }
            }

            if (parameter != null) {
                if (!parameters.ContainsKey(parameter))
                    parameters.Add(parameter, "true");
            }

            if (!String.IsNullOrEmpty(currentCommand)) {
                if (!this.Commands.ContainsKey(currentCommand)) {
                    this.Commands.Add(currentCommand, parameters);
                }
            } 

            if (!String.IsNullOrEmpty(currentPlugin)) {
                if (!this.Plugins.ContainsKey(currentPlugin)) {
                    this.Plugins.Add(currentPlugin, parameters);
                }
            }
        }
    }
}
